<?php
class Products_Images_Model extends MY_Model
{
    public function __construct()
    {
        parent::__construct();
    }

    public function add_image($data = array())
    {
        $images = $this->get_images(array('product_id' => $data['products_id'], 'last_row' => TRUE));
        $data['position'] = (isset($images->position)) ? $images->position + 1 : 1;   // Chưa tồn tại ảnh trong product thì gán ảnh mới = 1
        $this->db->insert('product_images', $data);

        if ($this->db->affected_rows()==1)
        {
            $id = $this->db->insert_id();
            return $id;
        }

        return NULL;
    }

    private function _set_where_conditions($options = array())
    {
        // WHERE
        if (isset($options['id']))
            $this->db->where('id', $options['id']);
        if (isset($options['product_id']))
            $this->db->where('products_id', $options['product_id']);
        if (isset($options['position']))
            $this->db->where('position', $options['position']);
    }

    function get_images($options = array())
    {
        $this->_set_where_conditions($options);
        
        // ORDER
        if (isset($options['order']))
            $this->db->order_by($options['order']);
        else
            $this->db->order_by('position');

        // GET
        $query = $this->db->get('product_images');

        if($query->num_rows() > 0){
            
            // RETURN
            if (isset($options['id']))
            {
                return $query->row(0);
            }
            if(isset($options['last_row']))
            {
                return $query->last_row();
            }
            return $query->result();
        }else{
            return NULL;
        }
        
    }

    function delete_image($id = 0, $image_path = '')
    {
        $image = $this->get_images(array('id' => $id));
        if (is_object($image))
        {

            // xoa file vat ly
            // 1. main image
            if (file_exists($image_path . $image->image_name))
                unlink($image_path . $image->image_name);
            // 2. thumbnail image
            if (file_exists($image_path . 'thumbnails/' . $image->image_name))
                unlink($image_path . 'thumbnails/' . $image->image_name);
            // 3. small image
            if (file_exists($image_path . 'smalls/' . $image->image_name))
                unlink($image_path . 'smalls/' . $image->image_name);

            // xoa trong db
            $str = 'UPDATE ' . $this->db->dbprefix. 'product_images set `position` = `position` - 1 where products_id = ' . $image->products_id . ' AND position > ' . $image->position;
            $this->db->query($str);
            
            $this->db->where('id', $id);
            $this->db->delete('product_images');
        }
        return NULL;
    }

    function delete_all_images($id = 0, $image_path = '')
    {
        $images = $this->get_images(array('product_id' => $id));

        if(isset($images)){
            foreach($images as $image)
            {
                // 1. main image
                if (file_exists($image_path . $image->image_name))
                    unlink($image_path . $image->image_name);
                // 2. thumbnail image
                if (file_exists($image_path . 'thumbnails/' . $image->image_name))
                    unlink($image_path . 'thumbnails/' . $image->image_name);
                // 3. small image
                if (file_exists($image_path . 'smalls/' . $image->image_name))
                    unlink($image_path . 'smalls/' . $image->image_name);
            }

            // xoa trong db
            $this->db->where('products_id', $id);
            $this->db->delete('product_images');
        }else{
            return NULL;
        }
        
    }
    
    public function upload_image_1($product_id = 0, $product_name='', $image_path = '')
    {
        $this->load->library('image_lib');
        $targetFolder = '/images/uploads'; // Relative to the root
        if (!empty($_FILES)) 
        {
            $tempFile = $_FILES['Filedata']['tmp_name'];
            $targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder;
            $targetFile = rtrim($targetPath,'/') . '/' . $_FILES['Filedata']['name'];

            // Validate the file type
            $fileTypes = array('jpg','jpeg','gif','png','JPG','JPEG','GIF','PNG'); // File extensions
            $fileParts = pathinfo($_FILES['Filedata']['name']);
            if (in_array($fileParts['extension'],$fileTypes)) 
            {
                move_uploaded_file($tempFile,$targetFile);

                $size = array();
                $size = getimagesize($targetFile);

                $data_upload = array('image_width' => $size[0], 'image_height' => $size[1]);

                $config['image_library'] = 'gd2';
                $config['source_image'] = './images/uploads/' . $_FILES['Filedata']['name'];
                $config['maintain_ratio'] = TRUE;

                $image_file_name = url_title($product_name, 'dash', TRUE) . '_' . $product_id . '_' . dechex((int)date('His'));
                $image_file_name .= '.' . $fileParts['extension'];
                // 1. Create the main image
                // setup the appropriate width & height
//                $config['width'] = 800;
//                $config['height'] = 600;
//                if ($data_upload['image_width'] < $data_upload['image_height']) {
//                    $config['width'] = 800;
//                    $config['height'] = 600;
//                }
                $config['new_image'] = $image_path . $image_file_name;
                $this->image_lib->initialize($config);

                $this->image_lib->resize();

                $this->image_lib->clear();

//                $config['source_image'] = $image_path . $image_file_name;
//                $config['wm_type'] = 'text';
//                $config['wm_text'] = DOMAIN_NAME;
//                $config['wm_font_size'] = '14';
//                $config['wm_font_color'] = 'ffffff';
//                $config['wm_vrt_alignment'] = 'bottom';
//                $config['wm_hor_alignment'] = 'right';
//                $this->image_lib->initialize($config);
//                $this->image_lib->watermark();
//                $this->image_lib->clear();

                // 2. Create the thumbnail image
                $thumb_size = 300;
                $_width = $data_upload['image_width'];
                $_height= $data_upload['image_height'];
                if ($_width > $_height)
                {
                    // wide image
                    $config['width'] = intval(($_width / $_height) * $thumb_size);
                    if ($config['width'] % 2 != 0)
                    {
                        $config['width']++;
                    }
                    $config['height'] = $thumb_size;
                    $img_type = 'landscape';
                }
                else if ($_width < $_height)
                {
                    // landscape image
                    $config['width'] = $thumb_size;
                    $config['height'] = intval(($_height / $_width) * $thumb_size);
                    if ($config['height'] % 2 != 0)
                    {
                        $config['height']++;
                    }
                    $img_type = 'portrait';
                }
                else
                {
                    // square image
                    $config['width'] = $thumb_size;
                    $config['height'] = $thumb_size;
                    $img_type = 'square';
                }
                $config['source_image'] = './images/uploads/' . $_FILES['Filedata']['name'];
                $config['new_image'] = $image_path . 'thumbnails/' . $image_file_name;
                $this->image_lib->initialize($config);
                $this->image_lib->resize();


                $conf_new = array(
                'image_library' => 'gd2',
                'source_image' => $config['new_image'],
                'create_thumb' => FALSE,
                'maintain_ratio' => FALSE,
                'width' => $thumb_size,
                'height' => $thumb_size
                );

                if ($img_type == 'landscape')
                {
                    $conf_new['x_axis'] = ($config['width'] - $thumb_size) / 2 ;
                    $conf_new['y_axis'] = 0;
                }
                else if($img_type == 'portrait')
                {
                    $conf_new['x_axis'] = 0;
                    $conf_new['y_axis'] = 0;
                }
                else
                {
                    $conf_new['x_axis'] = 0;
                    $conf_new['y_axis'] = 0;
                }

                $this->image_lib->initialize($conf_new);

                if ( ! $this->image_lib->crop())
                {
                    echo $this->image_lib->display_errors();
                }

                $this->image_lib->clear();

                // 3. Create the small image
                $config['width'] = 100;
                $config['height'] = 100;
                $config['source_image'] = $config['new_image'];
                $config['new_image'] = $image_path . 'smalls/' . $image_file_name;
                $this->image_lib->initialize($config);

                $this->image_lib->resize();

                $this->image_lib->clear();

                if (file_exists('./images/uploads/' . $_FILES['Filedata']['name']))
                        unlink('./images/uploads/' . $_FILES['Filedata']['name']);

                // Save to database
                $data = array(
                    'products_id'  => $product_id,
                    'image_name' => $image_file_name
                );
                $this->add_image($data);
                sleep(1);
            }
            else 
            {
                    echo 'Invalid file type.';
            }
        }
    }
    
}
?>